package com.zjitc.serviceedu.controller;


import com.zjitc.common.utils.RespBean;
import com.zjitc.serviceedu.entity.EduWatchhistory;
import com.zjitc.serviceedu.entity.vo.WatchHistoryVO;
import com.zjitc.serviceedu.service.EduWatchhistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 赵诚挚
 * @since 2024-10-19
 */
@RestController
@RequestMapping("/serviceedu/edu-watchhistory")
@Api(tags = "视频历史记录")
public class   EduWatchhistoryController {
    @InitBinder
    public void init(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }
    @Autowired
    private EduWatchhistoryService watchHistoryService;

    @PostMapping("/save")
    @ApiOperation("历史视频保存或更新功能")
    public String saveWatchHistory(
            @RequestParam Long userId,
            @RequestParam Long vid,
            @RequestParam Long courseId,
            @RequestParam String videoId,
            @RequestParam Integer lastPosition) {

        watchHistoryService.saveOrUpdateWatchHistory(userId, vid, courseId, videoId, lastPosition);
        return "历史视频保存或更新成功";
    }

    @GetMapping("/get")
    @ApiOperation("保存历史记录功能")
    public RespBean getWatchHistory(@RequestParam Long userId, @RequestParam String videoId) {
        EduWatchhistory watchHistory = watchHistoryService.getWatchHistory(userId, videoId);
        return RespBean.success().data("watchHistory",watchHistory).msg("保存历史记录成功");
    }

    @GetMapping("/watchhistory/{userId}")
    @ApiOperation("查询历史记录功能")
    public RespBean getWatchHistory(@PathVariable Long userId) {
        List<WatchHistoryVO> watchHistoryList = watchHistoryService.getWatchHistoryList(userId);
        return RespBean.success().data("data", watchHistoryList).msg("查询历史记录成功");
    }

}

